package demo.practice.leetcode_msjd;

public class Msjd0105 {

    public static void main(String[] args) {
        Msjd0105 msjd0105= new Msjd0105();
        boolean b = msjd0105.oneEditAway("teacher", "bleacher");
        /**
         * "teacher"
         * "bleacher"
         */
        System.out.println(b);
    }

    public boolean oneEditAway(String first, String second) {
        if (first == null|| first.equals(""))
            return second == null || second.length() <= 1;

        if (second == null|| second.equals(""))
            return first.length() <= 1;

        if (first.length() - second.length() > 1 || first.length() - second.length() < -1)
            return false;

        for (int i = 0; i < first.length(); i++) {
            if (i<second.length()&&  first.charAt(i) != second.charAt(i)) {
                if (first.length() - second.length() == 1) {
                    for (int j = i + 1; j < first.length(); j++) {
                        if (first.charAt(j) != second.charAt(j - 1))
                            //1.刪除,second 少一个字符
                            //abcd
                            //acd
                            return false;
                    }
                } else if (first.length() == second.length()) {
                    for (int j = i + 1; j < first.length(); j++) {
                        if (first.charAt(j) != second.charAt(j))
                            //1.替换,字符一样多，直接跳過i就可以了
                            //abcd
                            //akcd
                            return false;
                    }
                } else if (first.length() - second.length() == -1) {
                    for (int j = i; j < first.length(); j++) {
                        if (first.charAt(j) != second.charAt(j + 1))
                            //1.插入一個,second 多一个字符
                            //acd
                            //abcd
                            return false;
                    }
                }

                break;
            }
        }

        return true;

    }


}
